// 
//  Results.m
//  Test_CD_SL_2
//
//  Created by Timothy Costa on 9/18/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Results.h"


@implementation Results 

@dynamic dateAdded;
@dynamic totalScore;
@dynamic tries;
@dynamic lastScore;
@dynamic card;


-(void) awakeFromInsert
{
	self.tries = [NSNumber numberWithInt:0];
	self.totalScore = [NSNumber numberWithFloat:0.0];
	self.lastScore = [NSNumber numberWithFloat:0.0];
	self.dateAdded = [NSDate date];
}

-(void) updateScore: (float) newScore
{
	// Update tries
	int oldTries = [self.tries intValue];
	int newTries = oldTries+1;
	self.tries = [NSNumber numberWithInt:newTries];
	self.lastScore = [NSNumber numberWithFloat:newScore];

	// avoid multiply/divide by 0!
	if (newTries == 1){
		self.totalScore = [NSNumber numberWithFloat:newScore];
		return;
	}
	
	// update scores	
	float oldTotalScore = [self.totalScore floatValue];
	float newTotalScore = ( (oldTotalScore * oldTries) + newScore ) / newTries;
	
	self.totalScore = [NSNumber numberWithFloat:newTotalScore];	
}
@end
